
package quotation.utils;

import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;

import java.net.Inet4Address;

public class SnowFlakeUtil {
    private static final long START_STMP = 1480166465631L;
    private static final long SEQUENCE_BIT = 12L;
    private static final long MACHINE_BIT = 5L;
    private static final long DATACENTER_BIT = 5L;
    private static final long MAX_DATACENTER_NUM = 31L;
    private static final long MAX_MACHINE_NUM = 31L;
    private static final long MAX_SEQUENCE = 4095L;
    private static final long MACHINE_LEFT = 12L;
    private static final long DATACENTER_LEFT = 17L;
    private static final long TIMESTMP_LEFT = 22L;
    private long dataCenterId;
    private long machineId;
    private long sequence = 0L;
    private long lastStmp = -1L;
    private static SnowFlakeUtil snowFlake;

    private SnowFlakeUtil(long dataCenterId, long machineId) {
        if (dataCenterId <= 31L && dataCenterId >= 0L) {
            if (machineId <= 31L && machineId >= 0L) {
                this.dataCenterId = dataCenterId;
                this.machineId = machineId;
            } else {
                throw new IllegalArgumentException("machineId can't be greater than MAX_MACHINE_NUM or less than 0");
            }
        } else {
            throw new IllegalArgumentException("datacenterId can't be greater than MAX_DATACENTER_NUM or less than 0");
        }
    }

    public synchronized long nextId() {
        long currStmp = this.getNewstmp();
        if (currStmp < this.lastStmp) {
            throw new RuntimeException("Clock moved backwards.  Refusing to generate id");
        } else {
            if (currStmp == this.lastStmp) {
                this.sequence = this.sequence + 1L & 4095L;
                if (this.sequence == 0L) {
                    currStmp = this.getNextMill();
                }
            } else {
                this.sequence = 0L;
            }

            this.lastStmp = currStmp;
            return currStmp - 1480166465631L << 22 | this.dataCenterId << 17 | this.machineId << 12 | this.sequence;
        }
    }

    private long getNextMill() {
        long mill;
        for(mill = this.getNewstmp(); mill <= this.lastStmp; mill = this.getNewstmp()) {
        }

        return mill;
    }

    private long getNewstmp() {
        return System.currentTimeMillis();
    }

    private SnowFlakeUtil() {
    }

    public static SnowFlakeUtil getInstance() {
        return snowFlake = snowFlake == null ? new SnowFlakeUtil(getDataCenterId(), getMachineId()) : snowFlake;
    }

    private static Long getMachineId() {
        try {
            String hostAddress = Inet4Address.getLocalHost().getHostAddress();
            int[] ints = StringUtils.toCodePoints(hostAddress);
            int sums = 0;
            int[] var3 = ints;
            int var4 = ints.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                int b = var3[var5];
                sums += b;
            }

            return (long)(sums % 32);
        } catch (Exception var7) {
            return RandomUtils.nextLong(0L, 31L);
        }
    }

    private static Long getDataCenterId() {
        try {
            int[] ints = StringUtils.toCodePoints(SystemUtils.getHostName());
            int sums = 0;
            int[] var2 = ints;
            int var3 = ints.length;

            for(int var4 = 0; var4 < var3; ++var4) {
                int i = var2[var4];
                sums += i;
            }

            return (long)(sums % 32);
        } catch (Exception var6) {
            return RandomUtils.nextLong(0L, 31L);
        }
    }
}
